package com.zmn.plat.business.impl.product.tariff.group;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import com.zmn.common.constant.GlobalConsts;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.zmn.common.utils.math.MathUtil;
import com.zmn.plat.business.interfaces.product.tariff.group.ProductTariffGroupBService;
import com.zmn.plat.manager.dictionary.MasterDict;
import com.zmn.plat.model.entity.product.tariff.group.ProductTariffGroup;
import com.zmn.plat.model.entity.product.tariff.group.ProductTariffGroupQuery;
import com.zmn.plat.model.entity.product.tariff.item.ProductTariffGroupItemQuery;
import com.zmn.plat.model.vo.product.tariff.group.ProductTariffGroupVO;
import com.zmn.plat.model.vo.product.tariff.item.ProductTariffGroupItemVo;
import com.zmn.plat.services.interfaces.product.tariff.group.ProductTariffGroupService;
import com.zmn.plat.services.interfaces.product.tariff.item.ProductTariffGroupItemService;

/**
 * @author linfeng
 * @description
 * @since 2019/11/25 16:29
 * @modifier
 **/
@Service
public class ProductTariffGroupBServiceImpl implements ProductTariffGroupBService {
    @Resource
    private ProductTariffGroupItemService productFaultItemService;
    @Resource
    private ProductTariffGroupService productFaultGroupService;

    @Override
    public List<ProductTariffGroupVO> listGroupVOByQuery(ProductTariffGroupQuery query) {
        List<ProductTariffGroupVO> productFaultGroups = productFaultGroupService.listByQuery(query);

        if (!CollectionUtils.isEmpty(productFaultGroups)) {
            List<ProductTariffGroupVO> groupVOS = new ArrayList<>(productFaultGroups.size());
            ProductTariffGroupItemQuery itemQuery = new ProductTariffGroupItemQuery();
            for (ProductTariffGroup group : productFaultGroups) {
                ProductTariffGroupVO groupVO = new ProductTariffGroupVO();
                BeanUtils.copyProperties(group, groupVO);
                itemQuery.setGroupId(group.getGroupId());
                itemQuery.setProductId(query.getProductId());
                itemQuery.setTariffId(query.getTariffId());
                List<ProductTariffGroupItemVo> list = productFaultItemService.listByQuery(itemQuery);
                list.forEach(e -> {
                    e.setExternalSettlementPriceYuan(narrow(e.getExternalSettlementPrice()));
                    e.setInternalSettlementPriceYuan(narrow(e.getInternalSettlementPrice()));
                    calPriceTable(e);
                    // e.setPartPriceYuan(narrow(e.getPartPrice()));
                    // e.setArtPriceYuan(narrow(e.getArtPrice()));
                    e.setPriceYuan(narrow(e.getPrice()));
                    // e.setServicePriceYuan(narrow(e.getServicePrice()));
                    e.setHourFeeYuan(narrow(e.getHourFee()));
                    e.setCheckFeeYuan(narrow(e.getCheckFee()));
                    e.setTrafficFeeYuan(narrow(e.getTrafficFee()));
                    e.setNegotiableMaxPriceYuan(narrow(e.getNegotiableMaxPrice()));
                    e.setNegotiableMinPriceYuan(narrow(e.getNegotiableMinPrice()));
                });
                groupVO.setItems(list);
                groupVOS.add(groupVO);
            }
            return groupVOS;
        }
        return null;
    }

    private void calPriceTable(ProductTariffGroupItemVo vo){
        // 明细1 一口价区分明细计算价格
        if(Objects.equals(vo.getPriceStandard(),GlobalConsts.YES)
                &&Objects.equals(vo.getPriceSplit(),GlobalConsts.YES)&&Objects.equals(vo.getPriceSplitType(),GlobalConsts.NO)&&Objects.nonNull(vo.getPrice())){
            BigDecimal onehundred = new BigDecimal(GlobalConsts.MONEY_YUAN_FEN_RATE);
            BigDecimal artRatio = Objects.isNull(vo.getArtRatio())?BigDecimal.ZERO:vo.getArtRatio().divide(onehundred);
            BigDecimal guaranteeRatio = Objects.isNull(vo.getGuaranteeRatio())?BigDecimal.ZERO:vo.getGuaranteeRatio().divide(onehundred);
            BigDecimal partRatio =  vo.getPartRatio().divide(onehundred);
            Integer fee = vo.getHourFee() + vo.getTrafficFee()+vo.getCheckFee();
            // 销售价
            BigDecimal price = new BigDecimal(vo.getPrice());
            // 配件费
            BigDecimal partRatioPrice = price.multiply(partRatio);
            BigDecimal artRatioPrice = price.multiply(artRatio);
            BigDecimal guaranteeRatioPrice = price.multiply(guaranteeRatio);
            // 技术服务费
            BigDecimal tecPrice = price.subtract(partRatioPrice).subtract(artRatioPrice).subtract(guaranteeRatioPrice).subtract(new BigDecimal(fee));

            vo.setPartPrice(partRatioPrice.intValue());
            vo.setArtPrice(artRatioPrice.intValue());
            vo.setGuaranteePrice(guaranteeRatioPrice.intValue());
            vo.setServicePrice(tecPrice.intValue());
            vo.setPartPriceYuan(narrow(vo.getPartPrice()));
            vo.setArtPriceYuan(narrow(vo.getArtPrice()));
            vo.setGuaranteePriceYuan(narrow(vo.getGuaranteePrice()));
            vo.setServicePriceYuan(narrow(vo.getServicePrice()));

        }
    }

    private String narrow(Object dividend) {
        if (Objects.isNull(dividend)) {
            return "";
        }
        double div = MathUtil.div(Integer.parseInt(dividend.toString()), MasterDict.COEFFICIENT_MAGNIFICATION, 2);
        BigDecimal b = new BigDecimal(div);
        return b.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
    }
}
